package com.unionx.unionxuf.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.unionx.unionxuf.entity.Settlestyle;
import com.unionx.unionxuf.entity.updateDTD;
import com.unionx.unionxuf.localdao.SettlestyleMapper;

@Service("LocalSettleService")
public class LocalSettleServiceImpl implements LocalSettleService {

	@Autowired
	private SettlestyleMapper  slm;
	@Override
	public Object insertSettleStyle(String sde, String st, String sb, String sn,String corpid) {
		// TODO Auto-generated method stub

		Map<String,Object> map =  new HashMap<String,Object>();
		
		int k=0;
		
		String str1[] =sde.split(",");
		String str2[] =st.split(",");
		String str3[] =sb.split(",");
		String str4[] =sn.split(",");
		
		if(str1.length>1){
		for(int i =0;i<str1.length;i++){
			Settlestyle s = new Settlestyle();
			
			if(str1[i]==""||str1[i]=="null"||str1[i]==null||str1[i].equals("1")){
				s.setSettlementcode("");
			}else{
				s.setSettlementcode(str1[i]);
			}
			
			if(str2[i]==""||str2[i]=="null"||str2[i]==null||str2[i].equals("1")){
				s.setSettlement("");
			}else{
				s.setSettlement(str2[i]);
			}
			
			if(str3[i]==""||str3[i]=="null"||str3[i]==null||str3[i].equals("1")){
				s.setSubjectcode("");
			}else{
				s.setSubjectcode(str3[i]);
			}
			
			if(str4[i]==""||str4[i]=="null"||str4[i]==null||str4[i].equals("1")){
				s.setSubjectname("");
			}else{
				s.setSubjectname(str4[i]);
			}
			
			s.setCropid(corpid);
			s.setIsend("1");
			s.setSettlementEnd("1");
			
			k = slm.insert(s);
			 map.put("status", k);
			
			}
		}else{
			
			Settlestyle s = new Settlestyle();
			
			if(str1[0]==""||str1[0]=="null"||str1[0]==null||str1[0].equals("1")){
				s.setSettlementcode("");
			}else{
				s.setSettlementcode(str1[0]);
			}
			
			if(str2[0]==""||str2[0]=="null"||str2[0]==null||str2[0].equals("1")){
				s.setSettlement("");
			}else{
				s.setSettlement(str2[0]);
			}
			
			if(str3[0]==""||str3[0]=="null"||str3[0]==null||str3[0].equals("1")){
				s.setSubjectcode("");
			}else{
				s.setSubjectcode(str3[0]);
			}
			if(str4[0]==""||str4[0]=="null"||str4[0]==null||str4[0].equals("1")){
				s.setSubjectname("");
			}else{
				s.setSubjectname(str4[0]);
			}
			
			s.setCropid(corpid);
			s.setIsend("1");
			s.setSettlementEnd("1");
		     k = slm.insert(s);
			 map.put("status", k);
		}

		

		 return map;
		 
	}
	@Override
	public int insert(Settlestyle settlestyle) {
		// TODO Auto-generated method stub
		return slm.insert1(settlestyle);
	}
	@Override
	public Object delSettleStyle(String scode, String cropid) {
		// TODO Auto-generated method stub
		Settlestyle s = new Settlestyle();
		s.setSettlementcode(scode);
		s.setCropid(cropid);
		s.setZhuangtai("0");
		
		int i= slm.deleteByS(s);
		Map<String,Object> map =new HashMap<String,Object>();
		map.put("status", i);
		return map;
	}
	@Override
	public int updatebyls(Settlestyle settlestyle) {
		// TODO Auto-generated method stub
		return slm.updateBylsh(settlestyle);
	}
	@Override
	public Settlestyle selectby(updateDTD up) {
		// TODO Auto-generated method stub
		return slm.selectby(up);
	}
	@Override
	public Object selectst(String cropid) {
		// TODO Auto-generated method stub
		List<Settlestyle> list =  slm.selectst(cropid);
		return list;
	}
	@Override
	public int updatess(Settlestyle settlestyle) {
		// TODO Auto-generated method stub
		return slm.updateByPrimaryKey(settlestyle);
	}
	@Override
	public Settlestyle selectbys(String string) {
		// TODO Auto-generated method stub
		return slm.selectbys(string);
	}
	@Override
	public Object saveSettle(Settlestyle s) {
		// TODO Auto-generated method stub
		
		Map<String,Object> map =  new HashMap<String,Object>();
		
		int k=0;
		int j=0;
		List<Settlestyle> ss = slm.selectBylsh(s.getIsno(),s.getCropid());
		if(ss.size()!=0){
			k=slm.updateByPrimaryKey(s);
		}else{
			j=slm.insert(s);
		}
		
		map.put("status", j);
		return map;
	}

}
